﻿Public Class Pay
    Inherits System.Web.UI.Page

    Public ConnectionString As String = ConfigurationManager.AppSettings("ConnectionString").ToString
    Public objData As New DatabaseClass(ConnectionString)
    Public objDataRead As New DatabaseClass(ConnectionString)
    Public objFunction As New FunctionClass
    Public objAlert As New AlertClass
    Public objConfig As New Configs
    Dim Sql As String = ""

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            loadData()
        End If
    End Sub

    ' Kiem tra du lieu va load data cua khach hang
    Public Sub loadData()
        Try
            ' Kiem tra don hang co san pham hay k, neu khong co redirect ve trang chu
            If Session("TotalBill") = Nothing Then
                Response.Redirect("\")
            End If
            If (Session("CusUserName") <> Nothing) Or (Session("CusUserName") <> "") Or (Session("CusFullname") <> Nothing) Or (Session("CusFullname") <> "") Then
                txtName.Text = Session("CusFullname")
                txtEmail.Text = Session("CusEmail")
                txtPhone.Text = Session("CusPhone")
                txtAddress.Text = Session("CusAddress")
            End If
            lblTotal.Text = objFunction.formatVNCurrency(Session("TotalBill"))
        Catch ex As Exception
        End Try
    End Sub

    ' xu ly su kien thanh toan, insert don hang moi
    Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        'MsgBox("anhdp")
        Dim idCus As Integer = 0
        Try
            idCus = Integer.Parse(Session("CusID").ToString)
        Catch ex As Exception

        End Try
        Try
            'create procedure spo_BillInsert
            '@BillName	nvarchar(200),
            '@BillEmail	nvarchar(50),
            '@BillAddress	nvarchar(100),
            '@BillPhone	nvarchar(15),
            '@BillDesc	nvarchar(500),
            '@BillTotal	float,
            '@CusID	int
            Sql = "spo_BillInsert "
            Sql &= objFunction.FieldUniCk(txtName.Text) & ", "
            Sql &= objFunction.FieldCheck(txtEmail.Text) & ", "
            Sql &= objFunction.FieldUniCk(txtAddress.Text) & ", "
            Sql &= objFunction.FieldCheck(txtPhone.Text) & ", "
            Sql &= objFunction.FieldUniCk(txtDesc.Text) & ", "
            Sql &= objFunction.ValueCheck(lblTotal.Text) & ", "
            Sql &= objFunction.ValueCheck(idCus)
            objData.ExeReader(Sql)
            If objData.dataRead.Read Then
                Dim idBill As Integer = 0
                idBill = Integer.Parse(objData.dataRead("BillID").ToString)
                Dim objDT As System.Data.DataTable
                objDT = Session("Cart")
                For Each objDR In objDT.Rows
                    'create procedure spo_BillDetailInsert
                    '@BillID	int,
                    '@ProID	int,
                    '@BillDetailCnt	int
                    Sql = "spo_BillDetailInsert "
                    Sql &= objFunction.ValueCheck(idBill) & ", "
                    Sql &= objFunction.ValueCheck(objDR("ProId").ToString) & ", "
                    Sql &= objFunction.ValueCheck(objDR("Quantity").ToString)
                    objDataRead.ExeReader(Sql)
                Next
            End If
            ' Xoa du lieu don hang vua gui di trong sesion
            ' Khoi tao bien Gio Hang
            Dim obj As New Cart
            ' kiem tra neu bang khong co du lieu thi moi tao 1 cai moi'
            obj.makeCart()
            objAlert.InfoBox(Page, "Đơn hàng của bạn đã gửi đi thành công, Xin cảm ơn bạn đã sử dụng dịch vụ.", "/")
        Catch ex As Exception

        End Try
    End Sub
End Class